using System;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using System.ComponentModel;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using YB.EnterpriseLibrary.XSLTManager.Configuration.Design.Properties;
using System.Drawing.Design;

namespace YB.EnterpriseLibrary.XSLTManager.Configuration.Design
{
    public class XSLTManagerSettingsNode : ConfigurationSectionNode
    {

        private XSLTManagerNode xsltManagerNode;
		private EventHandler<ConfigurationNodeChangedEventArgs> xsltManagerNodeRemovedHandler;

        public XSLTManagerSettingsNode()
            : base(Resources.DefaultXSLTManagerSettingsNodeName)
        {
			this.xsltManagerNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnXSLTManagerNodeRemoved);			
			
        }
    
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (null != xsltManagerNode)
                    {
                        xsltManagerNode.Removed -= xsltManagerNodeRemovedHandler;
                    }

                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        [ReadOnly(true)]
        public override string Name
        {
            get { return base.Name; }
        }

        [Required]
        [SRDescription("DefaultXSLTManagerDescription", typeof(Resources))]
        [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
        [ReferenceType(typeof(XSLTManagerNode))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public XSLTManagerNode DefaultXSLTManager
        {
            get { return xsltManagerNode; }
            set
            {
                xsltManagerNode = LinkNodeHelper.CreateReference<XSLTManagerNode>(xsltManagerNode,
                                                                                    value,
                                                                                    xsltManagerNodeRemovedHandler,
                                                                                    null);
            }
        }

        protected override void OnChildAdded(ConfigurationNodeChangedEventArgs e)
        {
            base.OnChildAdded(e);

        }

        private void OnXSLTManagerNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
        {
            this.xsltManagerNode = null;
        }
    }
}
